package saucaca.fragment;

import java.util.ArrayList;

import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;

import org.json.JSONException;

import saucaca.CustomView.PagerSlidingTabStrip;
import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.TvshowObject;
import saucaca.Slidingmenu.SlidingMenu;
import saucaca.Util.CMD;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;


public class TvShowFragment extends Fragment {
	private MainActivity mActivity = null;
	private PagerSlidingTabStrip tabs;
	ViewPager viewPager;
	TvshowAdapter TabAdapter;
	ArrayList<TvshowObject> listTvshow;
	ProgressBar progressBar;
	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//		mActivity.getActionBar().hide();
		View view = inflater.inflate(R.layout.viewpager_tabstrip, container, false);
		findViewById(view);
		new GetFromServices().execute("");
		mActivity.myActionBar.setMenu(R.string.menu_tvshow, false, true);
		return view;
	}
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		mActivity.myActionBar.setMenu(R.string.menu_tvshow, false, true);
	}

	public void findViewById(View view) {
		viewPager = (ViewPager) view.findViewById(R.id.viewPager);
		tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
		progressBar=(ProgressBar) view.findViewById(R.id.empty_loading_progress);
		progressBar.setVisibility(View.VISIBLE);
		tabs.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageSelected(int position) {
				switch (position) {
				case 0:
					mActivity.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
					break;
				default:
					mActivity.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
					break;
				}
			}

		});
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof MainActivity) {
			mActivity = (MainActivity) activity;
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (TabAdapter != null)
			TabAdapter = null;
		if (viewPager != null)
			viewPager = null;
	}

	class GetFromServices extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... param) {
			String str = WServiceLib.getTop(CMD.CMD_TVSHOW_TOP);
			// BaseEngine.showLogformLongString(str);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if(result!=null)
			try {
				listTvshow = JsonReader.readTvshow(result);
				if (listTvshow != null) {
					progressBar.setVisibility(View.GONE);
					TabAdapter = new TvshowAdapter(getChildFragmentManager(), listTvshow);
					final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
					viewPager.setPageMargin(pageMargin);
					viewPager.setAdapter(TabAdapter);
					viewPager.setCurrentItem(0);
					tabs.setViewPager(viewPager);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}
	public class TvshowAdapter extends FragmentStatePagerAdapter {
		ArrayList<TvshowObject> listtvshow;

		public TvshowAdapter(FragmentManager fm, ArrayList<TvshowObject> listtvshow) {
			super(fm);
			this.listtvshow = listtvshow;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return listtvshow.get(position).getTXNAME();
		}

		@Override
		public int getCount() {
			return listtvshow.size();
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public Fragment getItem(int i) {
			return new TvshowList("", "", listtvshow.get(i).getCATE_CODE());
		}
	}
}
